package com.zqxq.lottery.shedule;


import com.zqxq.common.constant.Result;
import com.zqxq.lottery.common.constant.HuaYangConstant;
import com.zqxq.lottery.config.YmlConfig;
import com.zqxq.lottery.controller.order.BonusCalcController;
import com.zqxq.lottery.controller.sys.SysConfigController;
import com.zqxq.lottery.entity.enums.LotteryIdEnum;
import com.zqxq.lottery.entity.po.sys.SysConfigPO;
import com.zqxq.lottery.mq.Sender;
import com.zqxq.lottery.service.order.IUserOrderService;
import com.zqxq.lottery.service.sys.ILotteryHnumService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class LotteryJob {
    @Autowired
    private YmlConfig ymlConfig;

    @Autowired
    private ILotteryHnumService lotteryHnumService;

    @Autowired
    private IUserOrderService userOrderService;

    @Autowired
    private BonusCalcController bonusCalcController;

    @Autowired
    private Sender sender;

    @Autowired
    private SysConfigController sysConfigController;

    private Boolean canRun(){
        return "true".equals(ymlConfig.getShedule());
    }

    //##############################双色球 21:15:00 ###############################
    /*@Scheduled(cron="0 30 19 * * ? ")
    public void executeCloseSSQ() {
        if(canRun()){
            log.info("自动任务--【双色球】关闭开始------------------------");
            try {
                lotteryHnumService.updateLotteryForClose(LotteryIdEnum.SSQ.getId());
            } catch (Exception e) {
                log.error("自动任务--【双色球】关闭失败：",e);
            }
        }
    }

    //##############################大乐透 20:30:00 ###############################
    @Scheduled(cron="0 30 19 * * ? ")
    public void executeCloseDLT() {
        if(canRun()){
            log.info("自动任务--【大乐透】关闭开始------------------------");
            try {
                lotteryHnumService.updateLotteryForClose(LotteryIdEnum.DLT.getId());
            } catch (Exception e) {
                log.error("自动任务--【大乐透】关闭失败：",e);
            }
        }

    }

    //##############################福彩3D 21:15:00 ###############################
    @Scheduled(cron="0 30 19 * * ? ")
    public void executeCloseFC() {
        if(canRun()){
            log.info("自动任务--【福彩3D】关闭开始------------------------");
            try {
                lotteryHnumService.updateLotteryForClose(LotteryIdEnum.FC.getId());
            } catch (Exception e) {
                log.error("自动任务--【福彩3D】关闭失败：",e);
            }
        }

    }

    //##############################排列3 20:30:00 ###############################
    @Scheduled(cron="0 30 19 * * ? ")
    public void executeClosePL() {
        if(canRun()){
            log.info("自动任务--【排列三】关闭开始------------------------");
            try {
                lotteryHnumService.updateLotteryForClose(LotteryIdEnum.PL.getId());
            } catch (Exception e) {
                log.error("自动任务--【排列三】关闭失败：",e);
            }
        }

    }

    //##############################七星彩 20:30:00 ###############################

    @Scheduled(cron="0 30 19 * * ? ")
    public void executeCloseQXC() {
        if(canRun()){
            log.info("自动任务--【七星彩】关闭开始------------------------");
            try {
                lotteryHnumService.updateLotteryForClose(LotteryIdEnum.QXC.getId());
            } catch (Exception e) {
                log.error("自动任务--【七星彩】关闭失败：",e);
            }
        }

    }*/

    @Scheduled(cron="0 0/15 * * * ? ")
    public void executeCalc() {
        if (canRun()) {
            Result<SysConfigPO> result= sysConfigController.queryConfigValue("","AUTOMATIC_AWARD");
            SysConfigPO sysConfigPO = result.getData();
            if("1".equals(sysConfigPO.getCval())){
                log.info("自动任务--开始开奖------------------------");
                try {
                    bonusCalcController.lotteryBonusCalc(21L);
                    sender.send(HuaYangConstant.MQ_AWARD_BET_JCZQ,"21",1000*60*1);
                } catch (Exception e) {
                    log.error("自动任务--开奖失败：", e);
                }
            }
        }
    }

    @Scheduled(cron="0 0/15 * * * ? ")
    public void executeCalcLQ() {
        if (canRun()) {
            Result<SysConfigPO> result= sysConfigController.queryConfigValue("","AUTOMATIC_AWARD");
            SysConfigPO sysConfigPO = result.getData();
            if("1".equals(sysConfigPO.getCval())){
                log.info("自动任务--开始开奖------------------------");
                try {
                    bonusCalcController.lotteryBonusCalc(24L);
                    sender.send(HuaYangConstant.MQ_AWARD_BET_JCLQ,"24",1000*60*1);
                } catch (Exception e) {
                    log.error("自动任务--开奖失败：", e);
                }
            }
        }
    }

    @Scheduled(cron="0 0/15 * * * ? ")
    public void executeCalcDJ() {
        if (canRun()) {
            Result<SysConfigPO> result= sysConfigController.queryConfigValue("","AUTOMATIC_AWARD");
            SysConfigPO sysConfigPO = result.getData();
            if("1".equals(sysConfigPO.getCval())){
                log.info("自动任务--开始开奖------------------------");
                try {
                    bonusCalcController.lotteryBonusCalc(10L);
                    sender.send(HuaYangConstant.MQ_AWARD_BET_DJ,"10",1000*60*1);
                } catch (Exception e) {
                    log.error("自动任务--开奖失败：", e);
                }
            }
        }
    }

}
